/*
 * Validator.java
 *
 * $Id: Validator.java,v 1.5 2009-06-19 12:41:49 juancarlos Exp $
 */
package org.ceteca.explica.core.util;

import java.io.File;
import java.math.BigInteger;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

/**
 * Utility class with methods for usual format validations. <br/>
 * 
 * @author Mario García García <mario@imagos.es> Copyright (c) 2007 Fundación
 *         Centro Tecnolóxico da Carne This program is free software: you can
 *         redistribute it and/or modify it under the terms of the GNU General
 *         Public License as published by the Free Software Foundation, either
 *         version 3 of the License, or (at your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
public class Validator {
	private static Validator instance = null;

	private Validator() {
	}

	/**
	 * Return the unique instance of the class Validator (Singleton pattern)
	 * 
	 * @return Validator, static and unique instance of the class Validator.
	 */
	public static Validator getInstance() {
		if (instance == null)
			instance = new Validator();

		return instance;
	}

	/**
	 * Check if the String is empty.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is empty; False otherwise
	 */
	public boolean isEmpty(String s) {
		if (s == null || s.length() <= 0)
			return true;
		else {
			String tmp = s.trim();
			return (tmp == null || tmp.length() <= 0);
		}
	}

	/**
	 * Check if the data is numeric (unsigned integer)
	 * 
	 * @param s
	 *            String to check
	 * @return boolean, True if the string is numeric; False otherwise
	 */
	public boolean isValidNumeric(String s) {
		boolean isValid = false;
		if (!isEmpty(s)) {
			String tmp = s.trim();
			try {
				BigInteger b = new BigInteger(tmp);
				isValid = true;
			} catch (NumberFormatException ex) {
				isValid = false;
			}
		} else
			isValid = false;

		return isValid;
	}

	/**
	 * Check if the string is a float numeric.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid float; False otherwise
	 */
	public boolean isValidFloat(String s) {
		boolean isValid = false;
		if (!isEmpty(s)) {
			String tmp = s.trim();
			try {
				Float.parseFloat(tmp);
				isValid = true;
			} catch (NumberFormatException ex) {
				isValid = false;
			}
		} else
			isValid = false;

		return isValid;
	}

	/**
	 * Check if the string is a date.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid date; False otherwise
	 */
	public boolean isValidDate(String s) {
		boolean isValid = false;
		try {
			String tmp = s.trim();
			SimpleDateFormat df = new SimpleDateFormat(
					InterfaceValidator.CONST_SHORT_DATE_PATTERN);
			Date d = df.parse(tmp);
			if (d != null)
				isValid = true;
			else
				isValid = false;
		} catch (Exception ex) {
			isValid = false;
		}

		return isValid;
	}

	/**
	 * Check if the string is a date with time.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid date time; False otherwise
	 */
	public boolean isValidDateTime(String s) {
		boolean isValid = false;
		try {
			String tmp = s.trim();
			SimpleDateFormat df = new SimpleDateFormat(
					InterfaceValidator.CONST_DATETIME_PATTERN);
			Date d = df.parse(tmp);
			if (d != null)
				isValid = true;
			else
				isValid = false;
		} catch (Exception ex) {
			isValid = false;
		}

		return isValid;
	}

	/**
	 * Check if the string is a time.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid time; False otherwise
	 */
	public boolean isValidHora(String s) {
		boolean isValid = false;
		try {
			String tmp = s.trim();
			SimpleDateFormat df = new SimpleDateFormat(
					InterfaceValidator.CONST_SHORT_TIME_PATTERN);
			Date d = df.parse(tmp);
			if (d != null)
				isValid = true;
			else
				isValid = false;
		} catch (Exception ex) {
			isValid = false;
		}

		return isValid;
	}

	/**
	 * Check if the string is a boolean.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid boolean; False otherwise
	 */
	public boolean isValidBoolean(String s) {
		String tmp = s.trim().toLowerCase();

		return Boolean.parseBoolean(tmp);
	}

	/**
	 * Check if the string is a unique character (letter, number or special
	 * character).
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid character; False otherwise
	 */
	public boolean isValidCharacter(String s) {
		String tmp = s.trim();

		return (tmp != null && tmp.length() == 1);
	}

	/**
	 * Check if the string is a unique character of type letter.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid letter; False otherwise
	 */
	public boolean isValidCaracterAlpha(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (isValidCharacter(tmp) && tmp
					.matches(InterfaceValidator.CONST_ALPHABETIC_PATTERN));
		}
	}

	/**
	 * Check if the string is a unique character of type digit.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid digit; False otherwise
	 */
	public boolean isValidDigit(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (isValidCharacter(tmp) && tmp
					.matches(InterfaceValidator.CONST_NUMERIC_PATTERN));
		}
	}

	/**
	 * Check if the string is a alphabetic string (formed by only letters).
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid alphabetic string; False
	 *         otherwise
	 */
	public boolean isValidAlpha(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (!isEmpty(tmp) && tmp
					.matches(InterfaceValidator.CONST_ALPHABETIC_PATTERN));
		}
	}

	/**
	 * Check if the string is a alphanumeric string (formed by only letters or
	 * digits).
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid alphanumeric string; False
	 *         otherwise
	 */
	public boolean isValidAlphaNum(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (!isEmpty(tmp) && tmp
					.matches(InterfaceValidator.CONST_ALPHANUMERIC_PATTERN));
		}
	}

	/**
	 * Check if the string matches the specified pattern.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string matches the pattern; False otherwise
	 */
	public boolean isValidPattern(String s, String pattern) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (!isEmpty(tmp) && tmp.matches(pattern));
		}
	}

	/**
	 * Check if the string is a phone number.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid phone number; False
	 *         otherwise
	 */
	public boolean isValidPhone(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (!isEmpty(tmp) && tmp
					.matches(InterfaceValidator.CONST_PHONENUMBER_PATTERN));
		}
	}

	/**
	 * Check if the string is a postal code.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid postal code; False
	 *         otherwise
	 */
	public boolean isValidPostalCode(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (!isEmpty(tmp) && tmp
					.matches(InterfaceValidator.CONST_POSTALCODE_PATTERN));
		}
	}

	/**
	 * Check if the string is an e-mail address.
	 * 
	 * @param s
	 *            String to check.
	 * @return boolean, True if the string is a valid e-mail; False otherwise
	 */
	public boolean isValidEmail(String s) {
		if (s == null || s.length() <= 0)
			return false;
		else {
			String tmp = s.trim();
			return (!isEmpty(tmp) && tmp
					.matches(InterfaceValidator.CONST_EMAIL_PATTERN));
		}
	}



	
	
	
	/**
	 * Check if the string is a valid DNI.
	 * 
	 * @param docType
	 *            String, Document type.
	 * @param docNum
	 *            String, Document number.
	 * @param docLet
	 *            String, Document letter.
	 * @return boolean, True if the strings form a valid DNI; False otherwise
	 */
	
	
	
	public boolean isValidDni(String docType, String docNum, String docLet) {
		if (docType == null || docType.length() <= 0 || docNum == null
				|| docNum.length() <= 0 || docLet == null
				|| docLet.length() <= 0)
			return false;
		else {
			// TO_DO: Implement validation
			return false;
		}
	}

	/**
	 * Check if the strings are a valid current account.
	 * 
	 * @param cccEnt
	 *            String, Bank entity of the current account.
	 * @param cccSuc
	 *            String, Bank branch of the current account.
	 * @param cccDc
	 *            String, Control digits of the current account.
	 * @param cccNum
	 *            String, Bank account number.
	 * @return boolean, True if the strings form a valid current account number;
	 *         False otherwise.
	 */
	public boolean isValidCcc(String cccEnt, String cccSuc, String cccDc,
			String cccNum) {
		boolean isValid = false;
		if (cccEnt == null || cccEnt.length() <= 0 || cccSuc == null
				|| cccSuc.length() <= 0 || cccDc == null || cccDc.length() <= 0
				|| cccNum == null || cccNum.length() <= 0)
			isValid = false;
		else {
			String tmp = cccEnt.trim() + cccSuc.trim() + cccDc.trim()
					+ cccNum.trim();
			isValid = (!isEmpty(tmp) && tmp
					.matches(InterfaceValidator.CONST_CCC_PATTERN));
			int dc1 = -1;
			int dc2 = -1;
			try {
				dc1 = Integer.parseInt(cccDc.substring(0, 1));
				dc2 = Integer.parseInt(cccDc.substring(1, 2));
			} catch (Exception ex) {
				isValid = false;
			}
			if (isValid)
				isValid = this.getControlDigitCCC("00" + cccEnt + cccSuc) == dc1;
			if (isValid)
				isValid = this.getControlDigitCCC(cccNum) == dc2;
		}

		return isValid;
	}

	/**
	 * Check if the first date comes after the second date.
	 * 
	 * @param date1
	 *            String, first date
	 * @param date2
	 *            String, second date
	 * @return boolean, True if date1 > date2; False otherwise.
	 */
	public boolean isDateAfter(String date1, String date2) {
		boolean isAfter = false;
		if (date1 == null || date1.length() <= 0 || date2 == null
				|| date2.length() <= 0)
			isAfter = false;
		else {
			String tmp1 = date1.trim();
			String tmp2 = date2.trim();
			SimpleDateFormat dfdc = new SimpleDateFormat(
					InterfaceValidator.CONST_SHORT_DATE_PATTERN);
			SimpleDateFormat dfdl = new SimpleDateFormat(
					InterfaceValidator.CONST_DATETIME_PATTERN);
			SimpleDateFormat dfh = new SimpleDateFormat(
					InterfaceValidator.CONST_SHORT_TIME_PATTERN);
			Date d1 = null;
			Date d2 = null;

			try {
				d1 = dfdc.parse(tmp1);
				d2 = dfdc.parse(tmp2);
			} catch (Exception ex) {
				d1 = null;
				d2 = null;
			}

			if (d1 == null || d2 == null) {
				try {
					d1 = dfdl.parse(tmp1);
					d2 = dfdl.parse(tmp2);
				} catch (Exception ex) {
					d1 = null;
					d2 = null;
				}
			}

			if (d1 == null || d2 == null) {
				try {
					d1 = dfh.parse(tmp1);
					d2 = dfh.parse(tmp2);
				} catch (Exception ex) {
					d1 = null;
					d2 = null;
				}
			}

			if (d1 == null || d2 == null)
				isAfter = false;
			else {
				isAfter = d1.after(d2);
			}
		}

		return isAfter;
	}

	/**
	 * Check if an integer number is in the range [min .. max].
	 * 
	 * @param num
	 *            String, numeric value
	 * @param max
	 *            int, top of the range.
	 * @param min
	 *            int, bottom of the range.
	 * @return boolean, True if min <= num <= max; False otherwise
	 */
	public boolean isNumberInRange(String num, int max, int min) {
		boolean isInRange = false;
		if (num == null || num.length() <= 0)
			isInRange = false;
		else {
			String tmp = num.trim();
			try {
				Integer i = new Integer(tmp);
				isInRange = i.intValue() >= min && i.intValue() <= max;
			} catch (Exception ex) {
				isInRange = false;
			}
		}

		return isInRange;
	}

	/**
	 * Check if a date is in the range [dateMin .. dateMax].
	 * 
	 * @param date
	 *            String, date value
	 * @param dateMax
	 *            String, top date of the range.
	 * @param dateMin
	 *            String, bottom date of the range.
	 * @return boolean, True if dateMin <= date <= dateMax; False otherwise
	 */
	public boolean isDateInRange(String date, String dateMax, String dateMin) {
		boolean isInRange = false;
		if (date == null || date.length() <= 0)
			isInRange = false;
		else {
			String tmp = date.trim();
			try {
				DateTime dt = new DateTime(tmp,
						InterfaceValidator.CONST_SHORT_DATE_PATTERN);

				DateTime dateIni = null;
				DateTime dateFin = null;
				if (dateMax != null && dateMax.length() > 0) {
					dateFin = new DateTime(dateMax.trim(),
							InterfaceValidator.CONST_SHORT_DATE_PATTERN);
				}
				if (dateMin != null && dateMin.length() > 0) {
					dateIni = new DateTime(dateMin.trim(),
							InterfaceValidator.CONST_SHORT_DATE_PATTERN);
				}

				isInRange = this.isDateInRange(dt, dateIni, dateFin);
			} catch (Exception ex) {
				isInRange = false;
			}
		}

		return isInRange;
	}

	/**
	 * Check if a date is in the range [dateMin .. dateMax].
	 * 
	 * @param date
	 *            Date, date value
	 * @param dateMax
	 *            Date, top date of the range.
	 * @param dateMin
	 *            Date, bottom date of the range.
	 * @return boolean, True if dateMin <= date <= dateMax; False otherwise
	 */
	public boolean isDateInRange(java.sql.Date date, String dateMax,
			String dateMin) {
		boolean isInRange = false;
		if (date == null)
			isInRange = false;
		else {
			try {
				DateTime d = new DateTime(date,
						InterfaceValidator.CONST_SHORT_DATE_PATTERN);

				DateTime dateIni = null;
				DateTime dateFin = null;
				if (dateMax != null && dateMax.length() > 0) {
					dateFin = new DateTime(dateMax.trim(),
							InterfaceValidator.CONST_SHORT_DATE_PATTERN);
				}
				if (dateMin != null && dateMin.length() > 0) {
					dateIni = new DateTime(dateMin.trim(),
							InterfaceValidator.CONST_SHORT_DATE_PATTERN);
				}

				isInRange = this.isDateInRange(d, dateIni, dateFin);
			} catch (Exception ex) {
				isInRange = false;
			}
		}

		return isInRange;
	}

	/**
	 * Check if a date is in the range [dateMin .. dateMax].
	 * 
	 * @param date
	 *            Date, date value
	 * @param dateMax
	 *            Date, top date of the range.
	 * @param dateMin
	 *            Date, bottom date of the range.
	 * @return boolean, True if dateMin <= date <= dateMax; False otherwise
	 */
	public boolean isDateInRange(java.sql.Date date, java.sql.Date dateMax,
			java.sql.Date dateMin) {
		boolean isInRange = false;
		if (date == null)
			isInRange = false;
		else {
			try {
				DateTime d = new DateTime(date,
						InterfaceValidator.CONST_SHORT_DATE_PATTERN);

				DateTime dateIni = null;
				DateTime dateFin = null;
				if (dateMin != null) {
					dateIni = new DateTime(dateMin,
							InterfaceValidator.CONST_SHORT_DATE_PATTERN);
				}
				if (dateMax != null) {
					dateFin = new DateTime(dateMax,
							InterfaceValidator.CONST_SHORT_DATE_PATTERN);
				}

				isInRange = this.isDateInRange(d, dateIni, dateFin);
			} catch (Exception ex) {
				isInRange = false;
			}
		}

		return isInRange;
	}

	/**
	 * Comprueba si una dateha est comprendida en el rango especificado.
	 * 
	 * @param date
	 *            DateTime, con la dateha a validar.
	 * @param dateMax
	 *            DateTime, con la dateha superior del rango.
	 * @param dateMin
	 *            DateTime, con la dateha inferior del rango.
	 * @return boolean, True si dateMin <= date <= dateMax; False en caso
	 *         contrario
	 */
	public boolean isDateInRange(DateTime date, DateTime dateMin,
			DateTime dateMax) {
		boolean isInRange = false;
		if (date == null || date.getDate() == null)
			isInRange = false;
		else {
			isInRange = true;
			if (dateMin != null && dateMin.getDate() != null)
				isInRange = isInRange
						&& date.getDate().compareTo(dateMin.getDate()) >= 0;
			if (dateMax != null && dateMax.getDate() != null)
				isInRange = isInRange
						&& date.getDate().compareTo(dateMax.getDate()) <= 0;
		}

		return isInRange;
	}

	/**
	 * Check if a float number is in the range [min .. max].
	 * 
	 * @param num
	 *            String, float numeric value
	 * @param max
	 *            double, top number of the range.
	 * @param min
	 *            double, bottom number of the range.
	 * @return boolean, True if min <= num <= max; False otherwise
	 */
	public boolean isNumberInRange(String num, double max, double min) {
		boolean isInRange = false;
		if (num == null || num.length() <= 0)
			isInRange = false;
		else {
			String tmp = num.trim();
			try {
				Float f = new Float(tmp);
				isInRange = f.doubleValue() >= min && f.doubleValue() <= max;
			} catch (Exception ex) {
				isInRange = false;
			}
		}

		return isInRange;
	}

	/**
	 * Check if an object is included in the range of valid objects (Collection
	 * of valid objects)
	 * 
	 * @param val
	 *            Object, object value.
	 * @param col
	 *            Object[], collection of allowed values.
	 * @return boolean, True if val in col; False otherwise
	 */
	public boolean isValidValueInCollection(Object val, Object[] col) {
		boolean isValid = false;
		if (val == null || col == null || col.length <= 0)
			isValid = false;
		else {
			isValid = false;
			for (int i = 0; i < col.length && !isValid; i++) {
				Object elem = col[i];
				isValid = elem.equals(val);
			}
		}

		return isValid;
	}

	private int getControlDigitCCC(String num) {
		int control = 0;
		for (int i = 0; i <= 9; i++) {
			int dig = -1;
			try {
				dig = Integer.parseInt(num.substring(i, i + 1));
				control += dig * InterfaceValidator.VAL_DIGIT_WEIGHTS_CCC[i];
			} catch (NumberFormatException ex) {
				return -1;
			}
		} // for

		control = 11 - (control % 11);
		// Si es 11 devolvemos 0
		if (control == 11)
			control = 0;
		// Si es 10 devolvemos 1
		else if (control == 10)
			control = 1;
		// Si no devolvemos control % 11

		return control;
	}

	/**
	 * Check if the data is a barcode
	 * 
	 * @param s
	 *            String to check
	 * @return boolean, True if the string is a valid barcode; False otherwise
	 */
	public boolean isValidBarcode(String s) {
		boolean isValid = false;

		isValid = this.isValidNumeric(s);

		// TO_DO: Check if it's a valid barcode

		return isValid;
	}

	/**
	 * Check if the data is a file path
	 * 
	 * @param s
	 *            String to check
	 * @return boolean, True if the string is a valid file path; False otherwise
	 */
	public boolean isValidFile(String s) {
		boolean isValid = false;
		String tmp = (s == null) ? "" : s.trim();

		if (!isEmpty(tmp)) {
			try {
				File f = new File(tmp);
				isValid = f.exists();
			} catch (Exception ex) {
				isValid = false;
			}
		} else
			isValid = false;

		return isValid;
	}

	public boolean isValidBankAccount(long idBank, long idBranch,
			long idControlNumber, long idAccountNumber) {

		DecimalFormat df1 = new DecimalFormat("0000");
		DecimalFormat df2 = new DecimalFormat("00");
		DecimalFormat df3 = new DecimalFormat("0000000000");
		String account = df1.format(idBank) + df1.format(idBranch)
				+ df2.format(idControlNumber) + df3.format(idAccountNumber);
		int dcCalculado = digitoControl(account);
		int dcCuenta = Integer.parseInt(account.substring(8, 9));
		if (dcCalculado == dcCuenta)
			return false;
		else
			return true;
	}



	public boolean isValidSpanishNif(String nif) {
		
		boolean esvalidonif = false;
		if (nif==null || nif.isEmpty())
			return false;
		String dni = nif.substring(0, nif.length() - 1);
		char letc = nif.charAt(nif.length() - 1);
		Character letca = new Character(letc);
		String let = letca.toString();

		String regla = "[0-9]";

		if (let.matches(regla)) {
			esvalidonif = false;
		} else {
			
			char letra = Converter.getInstance().letraNIF(dni);
			if (letra==letc) {
				esvalidonif = true;
			} else {
				esvalidonif = false;

			}
		}
		return esvalidonif;
	};

	
	public boolean isValidSpanishCif(String cif){
		 

	 
		Pattern cifPattern =
			Pattern.compile("([ABCDEFGHKLMNPQSabcdefghklmnpqs])(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)([abcdefghijABCDEFGHIJ0123456789])");
	 
		Matcher m = cifPattern.matcher(cif);
		if(m.matches()){
			int sumaPar = Integer.parseInt(m.group(3))+Integer.parseInt(m.group(5))+Integer.parseInt(m.group(7));
			int sumaDigito2 = ((Integer.parseInt(m.group(2))*2)% 10)+((Integer.parseInt(m.group(2))*2)/ 10);
			int sumaDigito4 = ((Integer.parseInt(m.group(4))*2)% 10)+((Integer.parseInt(m.group(4))*2)/ 10);
			int sumaDigito6 = ((Integer.parseInt(m.group(6))*2)% 10)+((Integer.parseInt(m.group(6))*2)/ 10);
			int sumaDigito8 = ((Integer.parseInt(m.group(8))*2)% 10)+((Integer.parseInt(m.group(8))*2)/ 10);
			int sumaImpar = sumaDigito2 +sumaDigito4 +sumaDigito6 +sumaDigito8 ;
			int suma = sumaPar +sumaImpar;
			int control = 10 - (suma%10);
			if (control==10)
				control=0;
			String letras = "JABCDEFGHI";
			if (m.group(1).equalsIgnoreCase("K") || m.group(1).equalsIgnoreCase("P") ||
				m.group(1).equalsIgnoreCase("Q") || m.group(1).equalsIgnoreCase("S")){
				if (m.group(9).equalsIgnoreCase(letras.substring(control,control+1)))
					return true;
				else
					return false;
			}
			else if (m.group(1).equalsIgnoreCase("A") || m.group(1).equalsIgnoreCase("B") ||
				m.group(1).equalsIgnoreCase("E") || m.group(1).equalsIgnoreCase("H")){
				if (m.group(9).equalsIgnoreCase(""+control))
					return true;
				else
					return false;
			}
			else{
				if (m.group(9).equalsIgnoreCase(letras.substring(control,control+1))||
					m.group(9).equalsIgnoreCase(""+control))
					return true;
				else
					return false;
			}
		}
		else
			return false;
	 
	}
	
	
	
	
	
	
	private int digitoControl(String valor) {
		int valores[] = { 1, 2, 4, 8, 5, 10, 9, 7, 3, 6 };
		int control = 0;
		for (int i = 0; i <= 9; i++)
			control += Integer.parseInt(valor.substring(i, i + 1)) * valores[i];
		control = 11 - (control % 11);
		if (control == 11)
			control = 0;
		else if (control == 10)
			control = 1;
		return control;
	}

	/**
	 *  	Asses where is a valid identity card (only numbers)
	 * 
	 * @param dni to validate
	 * @return true if a valid dni
	 */
	public boolean isValidDNI(String dni){
		
		Pattern dniPattern =
			Pattern.compile("\\d{8}");
	 
		Matcher m = dniPattern.matcher(dni);
		return m.matches();
	}
	
	
	/**
	 * Retturn the CVS tag id of the file.
	 */
	public static String getRevision() {
		return "$Id: Validator.java,v 1.5 2009-06-19 12:41:49 juancarlos Exp $";
	}
}
